import { Resolver, Query, Mutation, Args, Context } from '@nestjs/graphql';
import { UserService } from './user.service';
import { User } from './entities/user.entity';
import { UseGuards } from '@nestjs/common';
import { JwtAuthGuard } from '../auth/guard/jwt-auth.guard';
import { BaseResolver } from 'src/common/mongodb/BaseResolver';
import { CreateUserInput, UpdatePasswordInput } from './dto/inputDto';
import { UserDTO } from './dto';

@Resolver(() => User)
export class UserResolver extends BaseResolver<User, typeof UserDTO>
  (User, UserDTO, '后台用户') {
  constructor(private readonly userService: UserService) {
    super(userService);
  }

  @Query(() => User)
  @UseGuards(JwtAuthGuard)
  async account(@Context() ctx) {
    const user = ctx.req.user;
    const id = user.id;
    const data = await this.userService.findById(id);
    return data;
  }

  @Query(() => User, { name: 'loginUser' })
  @UseGuards(JwtAuthGuard)
  loginUser(@Args('email') email: string, @Args('password') password: string) {
    return this.userService.loginUser({ email, password });
  }

  @Mutation(() => User)
  @UseGuards(JwtAuthGuard)
  updateUserPwd(@Args('updateUserPwd') updaetPwd: UpdatePasswordInput) {
    return this.userService.updateUserPwd(updaetPwd.id, updaetPwd);
  }

  @Mutation(() => User, { name: `createUser`, description: `创建用户` })
  @UseGuards(JwtAuthGuard)
  async create(@Args(`createUserInput`, { type: () => CreateUserInput }) createInput: CreateUserInput) {
    return this.userService.createUserInfo(createInput);
  }
}
